package com.lottery.system.dao.mapper;

import com.lottery.system.dao.dataobject.Encrypt;
import com.lottery.system.dao.dataobject.UserDo;
import jakarta.validation.constraints.Size;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author: xiaoxie
 * create: 2024-09-02 22:15
 * @BelongsProject: lottery-system
 * @BelongsPackage: com.lottery.system.dao.mapper
 * description: 用户数据层
 */
@Mapper
public interface UserMapper {
    /**
     * @description: 查询用户名的数量
     * @author: xiaoxie
     * @date: 2024/9/2 22:32
     * @param: [userName]
     * @return: int
     **/
    @Select("select count(*) from user where user_name = #{userName}")
    int userNameUsed(@Param(value = "userName") String userName);
    /**
     * @description: 查询邮箱的数量
     * @author: xiaoxie
     * @date: 2024/9/2 22:32
     * @param: [userName]
     * @return: int
     **/
    @Select("select count(*) from user where email = #{email}")
    int emailUsed(@Param(value = "email") String email);
    /**
     * @description: 查询电话号码的数量
     * @author: xiaoxie
     * @date: 2024/9/2 22:32
     * @param: [userName]
     * @return: int
     **/
    @Select("select count(*) from user where phone_number = #{phone}")
    int phoneUsed(@Param(value = "phone") Encrypt encrypt);

    /**
     * @description: 插入数据
     * @author: xiaoxie
     * @date: 2024/9/3 11:56
     * @param: [userDO]
     * @return: void
     **/
    @Insert("insert into user (user_name, email, phone_number, password, identify,salt)" +
            " values (#{userName}, #{email}, #{phoneNumber}, #{password}, #{identify},#{salt})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void insert(UserDo userDO);

    /**
     * @description: 通过手机号查数用户据
     * @author: xiaoxie
     * @date: 2024/9/6 22:51
     * @param: [encrypt]
     * @return: com.lottery.system.dao.dataobject.UserDo
     **/
    @Select("select id,email,identify,password,salt,user_name,phone_number from user" +
            " where phone_number = #{phone}")
    UserDo userInfoByPhone(Encrypt encrypt);
    /**
     * @description: 通过邮箱查用户数据
     * @author: xiaoxie
     * @date: 2024/9/6 22:51
     * @param: [loginName]
     * @return: com.lottery.system.dao.dataobject.UserDo
     **/
    @Select("select id,email,identify,password,salt,user_name,phone_number from user " +
            " where email = #{loginName}")
    UserDo userInfoByEmail(String loginName);
    /**
     * @description: 通过身份信息获得人员数据列表
     * @author: xiaoxie
     * @date: 2024/9/6 22:52
     * @param: [identify]
     * @return: java.util.List<com.lottery.system.dao.dataobject.UserDo>
     **/
    @Select("<script>" +
            " select id, email, identify, password, salt, user_name, phone_number from user" +
            " <if test=\"identify!=null\">" +
            "    where identify = #{identify}" +
            " </if>" +
            " order by id desc" +
            " </script>")
    List<UserDo> getUserInfoByIdentify(String identify);

    /**
     * @description: 查询列表中存在的用户id
     * @author: xiaoxie
     * @date: 2024/9/9 11:42
     * @param: [userIds]
     * @return: java.util.List<java.lang.Long>
     **/
    @Select("<script>" +
                  " select id from user" +
                  " where id in" +
                  " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
                  " #{item}" +
                  " </foreach>" +
                  " </script>")
    List<Long> exitsIds(@Param(value = "items") List<Long> userIds);

    /**
     * @description: 根据id列表查询用户信息列表
     * @author: xiaoxie
     * @date: 2024/9/13 10:52
     * @param: [list]
     * @return: java.util.List<com.lottery.system.dao.dataobject.UserDo>
     **/
    @Select("<script>" +
            " select id,email,identify,phone_number,user_name from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')'>" +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDo> getUserInfoByIds(@Param(value = "items") List<Long> ids);
}
